/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validacao;

import dados.GerenciadorDados;
import dados.ValidadorXml;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Emanoel
 */
public class GerenciadorSemantico {

    private static ArrayList<ConfigValidacaoSemantica> configs;

    public static ArrayList<ConfigValidacaoSemantica> carregarDefinicoesDB() throws ParserConfigurationException, IOException {
        ArrayList<ConfigValidacaoSemantica> retorno = new ArrayList<ConfigValidacaoSemantica>();
        if (ValidadorXml.validar("conf.xsd", "semanticconfig.xml")) {
            try {//D:\\Pessoal\\UPE\\2008.1\\Proj. de Compiladores\\Xml2SqlCompiler\\src\\java\\validacao\\
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse("semanticconfig.xml");
                NodeList tables = doc.getElementsByTagName("ns5:table");
                Element db = null;
                ConfigValidacaoSemantica def = null;

                for (int i = 0; i < tables.getLength(); i++) {
                    db = (Element) tables.item(i);
                    def = new ConfigValidacaoSemantica();
                    def.setName(db.getElementsByTagName("ns5:tablename").item(0).getTextContent());
                    NodeList columns = db.getElementsByTagName("ns5:column");
                    for (int j = 0; j < columns.getLength(); j++) {
                        def.getColumns().add(columns.item(j).getTextContent());
                        retorno.add(def);
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(GerenciadorDados.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        configs = retorno;
        return configs;
    }
}
